/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.games.epkb.controller;

import it.polimi.games.epkb.controller.exceptions.NonexistentEntityException;
import it.polimi.games.epkb.controller.exceptions.PreexistingEntityException;
import it.polimi.games.epkb.entityClasses.CpuCorePower;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import it.polimi.games.epkb.entityClasses.Cpu;
import it.polimi.games.epkb.entityClasses.CpuCore;
import javax.transaction.UserTransaction;

/**
 *
 * @author plebani
 */
public class CpuCorePowerJpaController implements Serializable {

    public CpuCorePowerJpaController(EntityManagerFactory emf) {
        this.utx = null;
        this.emf = emf;
    }

    public CpuCorePowerJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(CpuCorePower cpuCorePower) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cpu idCPU = cpuCorePower.getIdCPU();
            if (idCPU != null) {
                idCPU = em.getReference(idCPU.getClass(), idCPU.getId());
                cpuCorePower.setIdCPU(idCPU);
            }
            CpuCore idCore = cpuCorePower.getIdCore();
            if (idCore != null) {
                idCore = em.getReference(idCore.getClass(), idCore.getId());
                cpuCorePower.setIdCore(idCore);
            }
            em.persist(cpuCorePower);
            if (idCPU != null) {
                idCPU.getCpuCorePowerCollection().add(cpuCorePower);
                idCPU = em.merge(idCPU);
            }
            if (idCore != null) {
                idCore.getCpuCorePowerCollection().add(cpuCorePower);
                idCore = em.merge(idCore);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCpuCorePower(cpuCorePower.getId()) != null) {
                throw new PreexistingEntityException("CpuCorePower " + cpuCorePower + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CpuCorePower cpuCorePower) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CpuCorePower persistentCpuCorePower = em.find(CpuCorePower.class, cpuCorePower.getId());
            Cpu idCPUOld = persistentCpuCorePower.getIdCPU();
            Cpu idCPUNew = cpuCorePower.getIdCPU();
            CpuCore idCoreOld = persistentCpuCorePower.getIdCore();
            CpuCore idCoreNew = cpuCorePower.getIdCore();
            if (idCPUNew != null) {
                idCPUNew = em.getReference(idCPUNew.getClass(), idCPUNew.getId());
                cpuCorePower.setIdCPU(idCPUNew);
            }
            if (idCoreNew != null) {
                idCoreNew = em.getReference(idCoreNew.getClass(), idCoreNew.getId());
                cpuCorePower.setIdCore(idCoreNew);
            }
            cpuCorePower = em.merge(cpuCorePower);
            if (idCPUOld != null && !idCPUOld.equals(idCPUNew)) {
                idCPUOld.getCpuCorePowerCollection().remove(cpuCorePower);
                idCPUOld = em.merge(idCPUOld);
            }
            if (idCPUNew != null && !idCPUNew.equals(idCPUOld)) {
                idCPUNew.getCpuCorePowerCollection().add(cpuCorePower);
                idCPUNew = em.merge(idCPUNew);
            }
            if (idCoreOld != null && !idCoreOld.equals(idCoreNew)) {
                idCoreOld.getCpuCorePowerCollection().remove(cpuCorePower);
                idCoreOld = em.merge(idCoreOld);
            }
            if (idCoreNew != null && !idCoreNew.equals(idCoreOld)) {
                idCoreNew.getCpuCorePowerCollection().add(cpuCorePower);
                idCoreNew = em.merge(idCoreNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = cpuCorePower.getId();
                if (findCpuCorePower(id) == null) {
                    throw new NonexistentEntityException("The cpuCorePower with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CpuCorePower cpuCorePower;
            try {
                cpuCorePower = em.getReference(CpuCorePower.class, id);
                cpuCorePower.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cpuCorePower with id " + id + " no longer exists.", enfe);
            }
            Cpu idCPU = cpuCorePower.getIdCPU();
            if (idCPU != null) {
                idCPU.getCpuCorePowerCollection().remove(cpuCorePower);
                idCPU = em.merge(idCPU);
            }
            CpuCore idCore = cpuCorePower.getIdCore();
            if (idCore != null) {
                idCore.getCpuCorePowerCollection().remove(cpuCorePower);
                idCore = em.merge(idCore);
            }
            em.remove(cpuCorePower);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CpuCorePower> findCpuCorePowerEntities() {
        return findCpuCorePowerEntities(true, -1, -1);
    }

    public List<CpuCorePower> findCpuCorePowerEntities(int maxResults, int firstResult) {
        return findCpuCorePowerEntities(false, maxResults, firstResult);
    }

    private List<CpuCorePower> findCpuCorePowerEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CpuCorePower.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CpuCorePower findCpuCorePower(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CpuCorePower.class, id);
        } finally {
            em.close();
        }
    }

    public int getCpuCorePowerCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CpuCorePower> rt = cq.from(CpuCorePower.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
